//
//  HYCPushAllFreetroller.m
//  FamilyTravel
//
//  Created by dllo on 15/10/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCPushAllFreetroller.h"
#import "HYCHeadDocument.h"


#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HYCPushAllFreetroller ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, retain) MBProgressHUD *hud;
@end

@implementation HYCPushAllFreetroller
- (void)dealloc {
    [_arr release];
    [_tableView release];
    [_hud release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = [NSMutableArray array];
    self.page = 1;
    
    
    
    
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT- 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"加载中...";
    
    [self createData];
    
    // 上拉刷新下拉加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HeaderAction)];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.page++;
        [self createData];
        
    }];
    
}
-(void)HeaderAction{
    [self.arr removeAllObjects];
    [self createData];
}

- (void)createData {
    NSString *strOne = [NSString stringWithFormat:@"http://open.qyer.com/qyer/discount/local_discount?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&id=%@", self.str];
    
    NSString *strUrl = [NSString stringWithFormat:@"&order=2&page=%ld", (long)(long)self.page];
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@",strOne, strUrl, @"&product_type=2410&time=1&track_app_channel=App%2520Store&track_app_version=6.7&track_device_info=iPhone%25204%28ChinaMobile%2CChinaUnicom%29&track_deviceid=AB5B28ED-8670-4190-873A-F09EFE3EB3E8&track_os=ios%25207.1.2&type=1&v=1"];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection  sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        for (NSDictionary *tempDic in dic[@"data"][@"list"]) {
            HYCPushAllFreeModel *model = [[HYCPushAllFreeModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.arr addObject:model];
            [model release];
            
        }
        
        [self.hud removeFromSuperview];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return HEIGHT / 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    HYCPushAllFreeCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[HYCPushAllFreeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    
    if (self.arr.count != 0) {
        HYCPushAllFreeModel *model = self.arr[indexPath.row];
        cell.model = model;
        cell.layer.shadowColor = [[UIColor blackColor]CGColor];
        cell.alpha = 0;
        [UIView beginAnimations:@"rotation" context:NULL];
        [UIView setAnimationDuration:1];
        cell.alpha = 1;
        [UIView commitAnimations];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.arr.count != 0) {
        // 点击方法;
        HYCPushRecommendViewController *pRVC = [[HYCPushRecommendViewController alloc] init];
        pRVC.hidesBottomBarWhenPushed = YES;
        
        NSString *str = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_android&track_app_version=6.2&track_deviceid=866818029530472&track_user_id=6904760", [self.arr[indexPath.row] number]];
        
        pRVC.str = str;
        [self.navigationController pushViewController:pRVC animated:YES];
        pRVC.title = @"折扣详情";
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
